//
//  WmcFeedbackCell.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/1.
//

import UIKit

class WmcFeedbackCell: WmcBaseTableCell {
    
    var textDidChange: ((String?) -> Void)?
    
    lazy var rightDisTitleLabel: UILabel = {
        let label = UILabel.YP.Create(text:"请选择",
                                      font: 17,
                                      color: UIColor.wmc_grayH2_323233,
                                      alignment: .right)
        return label
    }()
    
    lazy var textField: UITextField = {
        let textField = UITextField()
        textField.attributedPlaceholder = "请输入".yp.makeAttributed({ make in
            make.set(foregroundColor:UIColor.wmc_grayH8_8A8A99)
        })
        textField.font = 17.wmc_font
        textField.textColor = .wmc_grayH2_323233
        textField.textAlignment = .right
        textField.returnKeyType = .done
        textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textField
    }()
    
    override func makeUI() {
        titleLabel.textColor = .wmc_grayH2_323233
        titleLabel.font = 17.wmc_font
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(Wmc_standardEdge.left)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        addSubview(arrowImageView)
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-Wmc_standardEdge.right)
            make.size.equalTo(12)
        }
        
        addSubview(rightDisTitleLabel)
        rightDisTitleLabel.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(arrowImageView.snp.left).offset(-8)
            make.left.equalTo(contentView.snp.centerX)
        }
        
        addSubview(textField)
        textField.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.right.equalTo(arrowImageView)
            make.left.equalTo(contentView.snp.centerX)
        }
    }
    
    func configModel(_ model: WmcFeedbackVM.CellModel) {
        titleLabel.text = model.title
        // 样式
        arrowImageView.isHidden = true
        rightDisTitleLabel.isHidden = true
        textField.isHidden = true
        switch model.style {
        case .arrow:
            rightDisTitleLabel.isHidden = false
            arrowImageView.isHidden = false
            if let content = model.content {
                rightDisTitleLabel.text = content
            }
        case .input: break
        case .textField:
            textField.isHidden = false
        }
    }
    
    @objc private func textFieldDidChange(_ sender: UITextField) {
        guard let text = sender.text else {
            textDidChange?(nil)
            return
        }
        
        if text.count > 20 {
            let result = (text as NSString).substring(to: 20)
            textField.text = result
        }
        
        textDidChange?(textField.text)
    }
}
